import time
import urllib.request
from selenium import webdriver


# 打开浏览驱动器
driver = webdriver.Chrome()
driver.implicitly_wait(3)

# 将浏览器最大化
driver.maximize_window()
driver.get('https://www.baidu.com')

# 搜索“深圳”图片，并操作 百度一下
driver.find_element_by_id('kw').send_keys('深圳')
driver.find_element_by_id('su').click()
time.sleep(2)
driver.find_element_by_link_text('图片').click()

# 定位图片元素
Images = driver.find_elements_by_css_selector('div.imgbox img[class="main_img img-hover"]')

i = 0
for x in Images:
    url = x.get_attribute('data-imgurl')
    urllib.request.urlretrieve(url, str(i)+'img.jpg')
    i += 1
    if i == 5:
        break

# 关闭浏览器
driver.quit()
